package com.kuang.controller;

import com.kuang.pojo.Users;
import com.kuang.pojo.Books;
import com.kuang.service.UserService;
import com.kuang.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;

    @RequestMapping("/allUser")
    public String list(Model model){
        List<Users> list = userService.queryAllUser();
        model.addAttribute("list", list);
        return "allUser";
    }


    @RequestMapping("/toUserCheck")
    public String toUserCheck(){return "userCheck";}

    @RequestMapping("/userCheck")
    public String userCheck(Users user)
    {
        String loginName = user.getUsername();
        String loginPassword = user.getPassword();
        Users checkUser = userService.queryUserByUsername(loginName);
        String checkPassword ;
//        System.out.println(user);
        if(checkUser != null)
        {
            checkPassword = checkUser.getPassword();
            if(checkPassword.equals(loginPassword))
            {
                return "redirect:/student/studentIndex?studentSno=" + loginName;
            }
        }
        return "redirect:/";
    }

    @RequestMapping("/toTeacherCheck")
    public String toTeacherCheck(){return "teacherCheck";}

    @RequestMapping("/teacherCheck")
    public String teacherCheck(Users user)
    {
        String loginName = user.getUsername();
        String loginPassword = user.getPassword();
        Users checkUser = userService.queryUserByUsername(loginName);
        String checkPassword ;
        if(checkUser != null)
        {
            checkPassword = checkUser.getPassword();
            if(checkPassword.equals(loginPassword))
            {
                return "redirect:/teacher/teacherIndex?teacherTid=" + loginName;
            }
        }
        return "redirect:/";
    }

    @RequestMapping("/toRootCheck")
    public String toRootCheck(){return "rootCheck";}

    @RequestMapping("/rootCheck")
    public String rootCheck(Users user){
        String loginName = user.getUsername();
        String loginPassword = user.getPassword();
        Users checkUser = userService.queryUserByUsername(loginName);
        String checkPassword ;
        if(checkUser != null)
        {
            checkPassword = checkUser.getPassword();
            if(checkPassword.equals(loginPassword))
            {
                return "redirect:/root/rootIndex";
            }
        }
        return "redirect:/";
    }

}
